const app = getApp();
const api = require('../../utils/api');

Page({
    data: {
        shopId: '', //店铺id
        hasStore: '', //是否已经选择店铺
        noSwitchShop: false, //是否禁止切换店铺

        shopDetail: '', //店铺详情
        bannerList: [],
        showShare: false,
    },
    onLoad(options) {
        if(options.id) {
            this.setData({
                shopId: options.id,
                noSwitchShop: options.dir ? true : false
            });
        } else if(wx.getStorageSync('shopId')) { //读取缓存店铺id
            this.setData({
                shopId: wx.getStorageSync('shopId')
            });
        }

        this.getStoreInfo();
        this.getBannerList();

        //设置微信分享功能：可分享小程序和分享朋友圈
        wx.showShareMenu({
            withShareTicket: true,
            menus: ['shareAppMessage', 'shareTimeline']
        });
    },
    onShow() {
       
    },

    getStoreInfo() { //获取店铺信息
        api.post('/shop/detail?shopId=' + this.data.shopId)
            .then(res => {
                let detail = res;
                //整理营业时间
                let businessBeginTime = detail.businessBeginTime.split(':');
                let businessEndTime = detail.businessEndTime.split(':');
                detail.businessTime = businessBeginTime[0] + ':' + businessBeginTime[1] + '~' + businessEndTime[0] + ':' + businessEndTime[1];
                this.setData({
                    shopDetail: detail,
                    hasStore: true
                });
            })
            .catch(err => {
                if(err.code === '200022' || err.code == '100004') {
                    setTimeout(() => {
                        this.setData({
                            hasStore: false
                        });
                    }, 1000);
                }
            })
    },
    gotoChangeShop() { //切换店铺
        if(this.data.noSwitchShop) {
            return;
        }
        wx.navigateTo({
            url: '/pages/storeList/storeList',
        })
    },

    findStore() { //查找店铺（第三方跳转过来时）
        let params = wx.getStorageSync('shop');
        api.post(`/shop/channel?channelCode=${params.code}&channelSubCode=${params.subCode}`)
            .then(res => {
                wx.removeStorageSync('shop');
                if(res) {
                    wx.setStorageSync('shopId', res); //缓存店铺id
                    this.setData({
                        shopId: res,
                        hasStore: true,
                    });
                    this.getStoreInfo();
                } else if(this.data.shopId) {
                    //是否显示店铺信息
                    this.getStoreInfo();
                    this.setData({
                        hasStore: true
                    });
                } else {
                    this.setData({
                        hasStore: false
                    });
                }
            });
    },

    getBannerList() { //获取banner列表
        api.post(`/ad/ad/list?districtCode=0&moduleNo=shop-index&shopId=${this.data.shopId}`)
            .then(res => {
                this.setData({
                    bannerList: res
                });
            })
    },

    callPhone() { //拨打电话
        wx.makePhoneCall({
            phoneNumber: this.data.shopDetail.phone
        })
    },
    openMiniProgram(e) { //打开小程序
        let appId = e.currentTarget.dataset.id;
        wx.navigateToMiniProgram({
            appId: appId
        });
    },

    openShare() { //打开分享弹窗
        this.setData({
            showShare: true,
        });
    },
    closeShare() {
        this.setData({
            showShare: false,
        });
    },

    onShareAppMessage() {
        let path = '';
        if(this.data.shopId) {
            path = 'pages/storeDetail/storeDetail?id=' + this.data.shopId;
        } else {
            path = 'pages/index/index'
        }
        return {
            title: this.data.shopDetail.shopName ? this.data.shopDetail.shopName : '八桂小铺',
            desc: this.data.shopDetail.remark ? this.data.shopDetail.remark : '八桂小铺',
            path: path// 路径，传递参数到指定页面
        }
    }
})